<template>
  <el-card class="swiper-container">
    <template #header>
      <div style="display: flex;flex-direction: row">
        <!-- 添加查询框    -->
        <el-form :inline="true" :model="queryForm" class="demo-form-inline">
          <el-form-item label="店铺名">
            <el-input v-model="queryForm.shopName" placeholder="请输入店铺名" clearable/>
          </el-form-item>
        </el-form>
        <div class="header">
          <el-button type="primary" v-if="!isShop" :icon="Plus" @click="getCarousels">查询</el-button>
          <el-button type="primary" v-if="!isShop" :icon="Plus" @click="handleAdd">增加</el-button>
        </div>
      </div>

    </template>
    <el-table
        :load="state.loading"
        ref="multipleTable"
        :data="state.tableData"
        style="width: 100%"
    >
      <el-table-column
          label="店铺名"
          prop="shopName"
          width="200">
      </el-table-column>
      <el-table-column
          label="店铺地址"
          prop="shopAddress"
          width="200"
      />

      <el-table-column
          label="联系方式"
          prop="shopPhone"
          width="300"
      />
      <el-table-column
          prop="createTime"
          label="添加时间"
          width="300"
      >
        <template #default="scope">
          {{ formatDate(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column
          label="操作"
          width="300"
          fixed="right"
      >
        <template #default="scope">
          <div style="display: flex;flex-direction: row">
            <el-button type="primary" size="small" @click="handleEdit(scope.$index,scope.row)">编辑</el-button>
            <el-button v-if="scope.row.status == 0" type="primary" size="small"
                       @click="passShop(scope.$index,scope.row)">通过
            </el-button>
            <el-button v-if="scope.row.status == 0" type="primary" size="small"
                       @click="refuseShop(scope.$index,scope.row)">拒绝
            </el-button>
            <el-button type="danger" size="small" v-if="!isShop" @click="deleteOne(scope.$index,scope.row)">删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        background
        layout="prev, pager, next"
        :total="state.total"
        :page-size="state.pageSize"
        :current-page="state.currentPage"
        :page-count="state.pages"
        @current-change="changePage"
    />
  </el-card>
  <DialogAddShop ref="addShop" :type="state.type" :reload="getCarousels"/>
</template>

<script setup>
import {onMounted, reactive, ref, toRefs} from 'vue';
import {ElMessage} from 'element-plus';
import {Plus, Delete} from '@element-plus/icons-vue';
import {getShopDetail, createShop, updateShop, deleteShop, shopList, doRefuseShop, doPassShop} from "@/api/shop";

const queryForm = ref({
  shopName: ''
});

const addShop = ref();
const state = reactive({
  loading: false,
  tableData: [], // 数据列表
  currentPage: 1,
  pageSize: 10,
  type: 'add', // 操作类型,
  multipleSelection: [], // 选中项
  total: 0, // 总条数
  pages: 1
});
const isShop = ref(false)
onMounted(() => {
  getCarousels();
  if (localStorage.getItem('type') == '"shopOwner"') {
    isShop.value = true
  }
});
// 获取轮播图列表
const getCarousels = async () => {
  state.loading = true;
  await shopList({
    shopName: queryForm.value.shopName,
    pageNumber: state.currentPage,
    pageSize: state.pageSize,
  }).then((res) => {
    console.log('请求结果：', res)
    state.tableData = res.data.data.records;
    state.total = res.data.data.total;
    state.pages = res.data.data.pages;
    state.currentPage = res.data.data.current;
    state.loading = false;
  })
};

// 格式化日期
const formatDate = (date) => {
  if (!date) return '';
  return new Date(date).toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  });
};

// 添加轮播项
const handleAdd = () => {
  console.log('addShop', addShop.value);
  state.type = 'add';
  addShop.value.open();
};
// 修改轮播图
const handleEdit = (index, row) => {
  state.type = 'edit';
  addShop.value.open(row);
};

const passShop = (index, row) => {
  console.log('row = ', row);
  doPassShop(row.shopId)

}
const refuseShop = (index, row) => {
  doRefuseShop(row.shopId)
}
// 选中之后的change方法，一旦选项有变化，就会触发该方法。
const handleSelectionChange = (val) => {
  state.multipleSelection = val;
};
const changePage = (val) => {
  state.currentPage = val;
  getCarousels();
};
const deleteOne = (index, row) => {
  console.log(row)
  deleteShop(row.shopId).then((res) => {
    console.log(res)
    ElMessage.success('删除成功');
    getCarousels();
  })
  getCarousels()
};
</script>

<style>

</style>
